package com.navigation.slidablenavigation;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

import com.kogi.patterns.R;
import com.navigation.ImageAdapter;
import com.navigation.segmented.EnergyEntry;
import com.navigation.segmented.EnergyTotalComparator;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class SlidableNavigation extends Activity {

	private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11;
	private TextView t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;
	ArrayList<PatentsCountries> entries;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slidable_navigation);
		setTitle("slider");

		t1 = (TextView) findViewById(R.id.textslider1);
		t2 = (TextView) findViewById(R.id.textslider2);
		t3 = (TextView) findViewById(R.id.textslider3);
		t4 = (TextView) findViewById(R.id.textslider4);
		t5 = (TextView) findViewById(R.id.textslider5);
		t6 = (TextView) findViewById(R.id.textslider6);
		t7 = (TextView) findViewById(R.id.textslider7);
		t8 = (TextView) findViewById(R.id.textslider8);
		t9 = (TextView) findViewById(R.id.textslider9);
		t10 = (TextView) findViewById(R.id.textslider10);

		b1 = (Button) findViewById(R.id.buttonslider1);
		b2 = (Button) findViewById(R.id.buttonslider2);
		b3 = (Button) findViewById(R.id.buttonslider3);
		b4 = (Button) findViewById(R.id.buttonslider4);
		b5 = (Button) findViewById(R.id.buttonslider5);
		b6 = (Button) findViewById(R.id.buttonslider6);
		b7 = (Button) findViewById(R.id.buttonslider7);
		b8 = (Button) findViewById(R.id.buttonslider8);
		b9 = (Button) findViewById(R.id.buttonslider9);
		b10 = (Button) findViewById(R.id.buttonslider10);
		b11 = (Button) findViewById(R.id.buttonslider11);

		entries = new ArrayList<PatentsCountries>();
		readFile();
		data(2002);

		b1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b1.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(2002);
			}

		});
		b2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b2.setTextColor(Color.parseColor("#99ccff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(2001);

			}

		});
		b3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b3.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(2000);
			}

		});
		b4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b4.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(1999);
			}

		});
		b5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b5.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(1998);
			}

		});
		b6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b6.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(1997);
			}

		});
		b7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b7.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(1996);
			}

		});
		b8.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b8.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(1995);
			}

		});
		b9.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b9.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(1994);
			}

		});
		b10.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b10.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b11.setTextColor(Color.parseColor("#ffffff"));
				data(1993);
			}

		});
		b11.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				b11.setTextColor(Color.parseColor("#99ccff"));
				b2.setTextColor(Color.parseColor("#ffffff"));
				b1.setTextColor(Color.parseColor("#ffffff"));
				b4.setTextColor(Color.parseColor("#ffffff"));
				b5.setTextColor(Color.parseColor("#ffffff"));
				b3.setTextColor(Color.parseColor("#ffffff"));
				b7.setTextColor(Color.parseColor("#ffffff"));
				b8.setTextColor(Color.parseColor("#ffffff"));
				b9.setTextColor(Color.parseColor("#ffffff"));
				b6.setTextColor(Color.parseColor("#ffffff"));
				b10.setTextColor(Color.parseColor("#ffffff"));
				data(1992);
			}

		});

	}

	public void data(int year) {

		ArrayList<String> datos = new ArrayList<String>();

		Collections.sort(entries, new PatentsByYear(year));

		int i = 1;
		if (year == 2002)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value2002));
				i++;
			}
		else if (year == 2001)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value2001));
				i++;
			}
		else if (year == 2000)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value2000));
				i++;
			}
		else if (year == 1999)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value1999));
				i++;
			}
		else if (year == 1998)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value1998));
				i++;
			}
		else if (year == 1997)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value1997));
				i++;
			}
		else if (year == 1996)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value1996));
				i++;
			}
		else if (year == 1995)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value1995));
				i++;
			}
		else if (year == 1994)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value1994));
				i++;
			}
		else if (year == 1993)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value1993));
				i++;
			}
		else if (year == 1992)
			for (PatentsCountries entry : entries) {
				datos.add(String.format("%d. %s %d", i, entry.country,
						entry.value1992));
				i++;
			}

		t1.setText(datos.get(0));
		t2.setText(datos.get(1));
		t3.setText(datos.get(2));
		t4.setText(datos.get(3));
		t5.setText(datos.get(4));
		t6.setText(datos.get(5));
		t7.setText(datos.get(6));
		t8.setText(datos.get(7));
		t9.setText(datos.get(8));
		t10.setText(datos.get(9));

	}

	public void readFile() {
		InputStream in;

		ContextWrapper context = this;
		in = context.getResources().openRawResource(R.raw.patents);

		try {

			StringBuffer inLine = new StringBuffer();
			InputStreamReader isr = new InputStreamReader(in);

			BufferedReader inRd = new BufferedReader(isr);

			// obtener datos
			String text;

			int fi = 0;
			while ((text = inRd.readLine()) != null) {
				if (fi == 0) {
					String titulos[] = text.toString().split(",");

					setTitle(titulos[0]);
					fi = 1;
				} else {

					entries.add(PatentsCountries.getFromLine(text));
					

				}

			}
			in.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.v ("error",e.toString());
		}
		//Log.v("Omar", "este es el consola andorid");
	}

}
